package com.platform.gen.controller;

import com.platform.common.utils.StringUtils;
import com.platform.gen.model.GenTable;
import com.platform.gen.service.GenTableService;
import com.platform.gen.utils.GenUtils;
import com.platform.upms.model.json.Tip;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/gen")
public class GenTableController  {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(GenTableController.class);
    @Autowired
    GenTableService genTableService;


    @RequestMapping(value = {"list"})
    private String list(ModelMap modelMap) {
        List<GenTable> list = genTableService.findAlllist();
        modelMap.put("genList",list);
        return  "/gen/list.jsp";
    }

    /**
     * 添加生成列表页面（选择数据库中的表）
     * @param genTable
     * @param model
     * @return
     */
    @RequestMapping(value = "/add")
    private String add(GenTable genTable, Model model){
        List<GenTable> list = genTableService.findTableListFormDb(new GenTable());
        model.addAttribute("tableList",list);
        // 验证表是否存在
        if (StringUtils.isBlank(String.valueOf(genTable.getId())) && !genTableService.checkTableName(genTable.getName())){
            genTable.setName("");
        }
        // 获取物理表字段
        else{
            genTable = genTableService.getTableFormDb(genTable);
        }
        model.addAttribute("genTable", genTable);
        model.addAttribute("config", GenUtils.getConfig());
        return "/gen/add.jsp";
    }
    @RequestMapping(value = "/twoadd")

    public String twoadd(GenTable genTable, Model model){
        System.out.println(genTable.getName());
        List<GenTable> list = genTableService.findTableListFormDb(new GenTable());
        model.addAttribute("tableList",list);
        genTable = genTableService.getTableFormDb(genTable);
        model.addAttribute("genTable", genTable);
        model.addAttribute("config", GenUtils.getConfig());
        return "/gen/twoadd.jsp";
    }
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public Tip save(GenTable genTable, Model model) {
        try {
            // 验证表是否已经存在
            if (StringUtils.isBlank(genTable.getId()) && !genTableService.checkTableName(genTable.getName())) {
                genTable.setName("");
                return new Tip(1, "保存失败！！！已经存在");
            }
            genTableService.save(genTable);
            return  new Tip("保存成功");

/*
        addMessage(redirectAttributes, "保存业务表'" + genTable.getName() + "'成功");
*/
        } catch (Exception e) {
            return new Tip(1, "保存失败");
        }

    }
    @RequestMapping(value = "/del")
    @ResponseBody
    private Tip del(String[] ids){
        logger.debug(ids.toString());
        Tip tip = new Tip();
        try {
            genTableService.deleteDataByIds(ids);

        } catch(Exception e) {
            tip = new Tip(1);
        }
        return tip;


    }

}
